package com.wjm.databaseframework.sub_sqlite;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wjm.databaseframework.db.BaseDao;
import com.wjm.databaseframework.db.BaseDaoFactory;

public class BaseDaoSubFactory extends BaseDaoFactory {

    private static final BaseDaoSubFactory ourInstance = new BaseDaoSubFactory();

    public static BaseDaoSubFactory getOurInstance() {
        return ourInstance;
    }

    protected SQLiteDatabase subSqliteDatabase;

    protected BaseDaoSubFactory() {
    }


    public <T extends BaseDao<M>, M> T getSubDao(Class<T> daoClass, Class<M> entityClass) {
        BaseDao baseDao = null;
        if (map.get(PrivateDataBaseEnums.database.getValue()) != null) {
            return (T) map.get(PrivateDataBaseEnums.database.getValue());
        }
        Log.i("jett", "生成数据库文件的位置:" + PrivateDataBaseEnums.database.getValue());
        SQLiteDatabase sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(PrivateDataBaseEnums.database.getValue(), null);
        try {
            baseDao = daoClass.newInstance();
            baseDao.init(sqLiteDatabase, entityClass);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return (T) baseDao;
    }
}
